package p.jce;

import java.security.KeyFactory;
import java.security.KeyFactorySpi;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;

public class RKeyFactory {

	private static class X extends KeyFactory {

		protected X(KeyFactorySpi keyFacSpi, Provider provider, String algorithm) {
			super(keyFacSpi, provider, algorithm);
		}

	}

	public static KeyFactory getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException {
		return getInstance(algorithm);
	}

	public static KeyFactory getInstance(String algorithm, String provider) throws NoSuchAlgorithmException,
			NoSuchProviderException {
		if ("BC".equals(provider)) {
			try {
				return new X((KeyFactorySpi) BC.newInstance("KeyFactory", algorithm), null, algorithm);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		return KeyFactory.getInstance(algorithm, provider);
	}

	public static KeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException {
		try {
			return getInstance(algorithm, "BC");
		} catch (NoSuchProviderException e) {
			throw new RuntimeException(e);
		}
	}
}
